<template>
  <div class='page-container'>
    <el-form :model="queryParams" :rules="rules" ref="queryRef" :inline="true">
      <el-form-item label="岗位编码" prop="postCode">
        <el-input v-model="queryParams.postCode" placeholder="请输入岗位编码" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="queryParams.postName" placeholder="请输入岗位名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable style="width: 200px">
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" v-hasPermi="['system:post:add']" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" v-hasPermi="['system:post:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" v-hasPermi="['system:post:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" v-hasPermi="['system:post:export']">导出</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="postList.list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="岗位编号" align="center" prop="postId" />
      <el-table-column label="岗位编码" align="center" prop="postCode" />
      <el-table-column label="岗位名称" align="center" prop="postName" />
      <el-table-column label="岗位排序" align="center" prop="postSort" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm(postRef)">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { listPost, addPost } from "@/api/system/post";

/**
 * 模块引入
 */
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
/**
 * 数据
 */
let loading = ref(false);
const postList = reactive({ list: [] });
const open = ref(false)
const postRef = ref(null);

const title = ref("");
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});
const form = reactive({
  postId: undefined,
  postCode: undefined,
  postSort: 0,
  status: "0",
  remark: undefined
});
const rules = reactive({
  postName: [
    {
      required: true,
      trigger: 'blur',
    },
    {
      min: 3,
      max: 5,
      message: 'Length should be 3 to 5',
      trigger: 'blur',
      informType: 'warning',
    },
  ],
  postCode: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'blur',
    },
  ]
})

/**
 * 功能方法
 */
// 点击新增按钮
const handleAdd = function () {
  resetFileds()
  // 弹框 设置模态框的标题
  // 重置表单
  open.value = true;
  title.value = "添加岗位";

}
// 点击模态框提交按钮
const submitForm = function () {
  // 验证  提交表单
  if (title.value == "添加岗位") {

    postRef.value.validate(valid => {
      if (valid) {

        // 验证之后
        addPost(form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      } else {

      }


    });
  } else {
    // 修改的
  }
}

// 点击取消按钮
const cancel = function () {
  open.value = false;  //关闭弹窗
  resetFileds();

}
const resetFileds = function () {
  form.postName = undefined
  form.postId = undefined,
    form.postCode = undefined,
    form.postSort = 0,
    form.status = "0",
    form.remark = undefined
}
const handleQuery = function () {
  console.log('handleQuery')
  getList()
}
const resetQuery = function () {
  proxy.resetForm("queryRef");
  queryParams.pageNum = 1
  getList()
}
const getList = async function () {
  try {
    const result = await listPost(queryParams)
    postList.list = result.rows

  } catch (error) {

  }
}
getList();
// 复选框切换选择
const handleSelectionChange = function () { }
</script>
<style lang='scss' scoped>
.page-container {}
</style>